<#ftl/>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperPackage}.${mapperClassName}">
    <!--分页列表-->
    <select id="query" resultType="<#if need_saveDto>${voPackage}.${voClassName}<#else>${entityPackage}.${entity}</#if>"
            parameterType="${entityPackage}.${entity}">
        select * from ${table.name}
        <where>
            <#list table.fields as field>
                <#if field.propertyType=='String'>
                    <if test="${field.propertyName} != null and ${field.propertyName} != ''">
                        and ${field.name} = <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                    </if>
                <#else>
                    <if test="${field.propertyName} != null">
                        and ${field.name} = <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                    </if>
                </#if>
            </#list>
        </where>
    </select>
    <#if mapperPlus>
    <#else>
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="${entityPackage}.${entity}">
            <#list table.fields as field>
                <#if field.keyFlag><#--生成主键排在第一位-->
                    <id column="${field.name}" jdbcType="${field.metaInfo.jdbcType}" property="${field.propertyName}"/>
                </#if>
            </#list>

            <#list table.fields as field>
                <#if !field.keyFlag><#--生成普通字段 -->
                    <result column="${field.name}" jdbcType="${field.metaInfo.jdbcType}"
                            property="${field.propertyName}"/>
                </#if>
            </#list>
        </resultMap>

        <#if baseColumnList>
            <!-- 通用查询结果列 -->
            <sql id="Base_Column_List">
                <#list table.fields as field>${field.name}<#if field_has_next>,</#if></#list>
            </sql>

        </#if>

        <insert id="insert${entity}" parameterType="${entityPackage}.${entity}">
            insert into ${table.name}
            (<#list table.fields as field><#if !field.keyFlag><#--生成普通字段 -->${field.name}<#if field_has_next>,</#if></#if></#list>
            )
            values
            (<#list table.fields as field><#if field.propertyName="createTime"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
            )
        </insert>

        <insert id="batchInsert${entity}">
            <#if dbType=="oracle">BEGIN
            <foreach item="item" index="index" collection="list">
                insert into ${table.name}
                (<#list table.fields as field><#if !field.keyFlag><#--生成普通字段 -->${field.name}<#if field_has_next>,</#if></#if></#list>
                )
                values
                (<#list table.fields as field><#if field.propertyName="createTime"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
                );
            </foreach>
            END;
            <#else>
            insert into ${table.name}
            (<#list table.fields as field><#if !field.keyFlag><#--生成普通字段 -->${field.name}<#if field_has_next>,</#if></#if></#list>
            )
            values
            <foreach item="item" index="index" collection="list">
                (<#list table.fields as field><#if field.propertyName="createTime"><#if dbType=="oracle">sysdate<#else>NOW()</#if><#else> <#noparse>#{</#noparse>item.${field.propertyName},jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse></#if><#if field_has_next>,</#if></#list>
                )
                </#if>

        </insert>

        <delete id="delete${entity}ByPrimaryKey">
            delete from ${table.name} where
            <#list table.fields as field>
                <#if field.keyFlag>
                    ${field.name} = <#noparse>#{</#noparse>pk,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                </#if>
            </#list>

        </delete>

        <delete id="batchDelete${entity}ByPrimaryKeys">
            <foreach item="id" index="index" collection="pks">
                delete from ${table.name} where
                <#list table.fields as field>
                    <#if field.keyFlag>
                        ${field.name} = <#noparse>#{</#noparse>id,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                    </#if>
                </#list>

            </foreach>
        </delete>



        <select id="select${entity}ByPrimaryKey" resultMap="BaseResultMap">
            select * from ${table.name} where
            <#list table.fields as field>
                <#if field.keyFlag>
                    ${field.name} = <#noparse>#{</#noparse>pk,jdbcType=${field.metaInfo.jdbcType}<#noparse>}</#noparse>
                </#if>
            </#list>

        </select>

    </#if>
</mapper>
